<?php

namespace app\backend\controller;
//导入基类
use app\common\model\Reward;
use think\Controller;
use think\facade\Cache;
use think\Session;
use think\Db;
use app\common\model\Banner as bannerModel;
use app\common\model\Config as configModel;
use app\common\model\Notice as NoticeModel;
use app\common\model\Sendmsg as SendmsgModel;
use think\Validate;
use app\backend\model\AdminUser;
use app\backend\model\AdminRole;
class Config extends Controller
{	

	//管理员
	public function index(){
		$admin_list=AdminUser::all();
        $this->assign('admin_list',$admin_list);
        return $this->fetch();
	}


	public function addAdmin()
    {
        if (request()->isPost()) {

            $info = input('post.');

            $validate = Validate::make([
                'name' => 'require|chsAlphaNum',
                'password' => 'require|length:6,12'
            ], [], ['name' => '账号', 'password' => '密码']);

            if ($validate->check($info)) {
                $admin = AdminUser::create($info);
                return 1;
            } else {
               return $validate->getError();
            }

        }
        $list=AdminRole::all();
        return $this->fetch('',['list'=>$list]);
    }

    public function delete(){
        $id=input('post.id');
        $admin=AdminUser::destroy($id);
        return $admin;
    }
    
     public function editAdmin(){
        if (request()->isPost()) {

            $info = input('post.');
            if(empty(input('post.password'))){
             $info=request()->except('password');       
            }
            $edit = AdminUser::update($info);
            return $edit;
        }
        $id=input('get.id');
        $data = AdminUser::get($id);
        $list=AdminRole::all();
        // echo'<pre>';
        // var_dump($data);
        $this->assign('data', $data);
        $this->assign('list', $list);
       return $this->fetch();
    }

	public function banner(){

		return $this->fetch(); 
	}
	public function bannerlist(){
		$limit = request()->param('limit');
		$list = bannerModel::order('sort asc')->paginate($limit);
		$count = bannerModel::count();
		$data = ['code' => 0, 'msg' => '轮播图片列表', 'count' => $count, 'data' => $list->items()];
		return json($data);
	}
	/**
	 * 添加轮播图片
	 */
	public function addBanner()
	{
		if (request()->isPost()) {
			$infos = input('post.');
			$file = request()->file('titleFile');
			if($infos['sort'] == ''){
				return  $data = ['code' => 0, 'msg' => '请填写排序'];
			}

			if($file){
				// 移动到框架uploads/ 目录下
				$info = $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif'])->move( './uploads');
				if($info){
					// 成功上传后 获取上传信息
					// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
					$infos['img']='/uploads/'.$info->getSaveName();

				}else{
					// 上传失败获取错误信息
					return $file->getError();
				}
				$validate = Validate::make([
					'title' => 'require|token',
					'sort' => 'require',

				], [], ['title' => '标题', 'sort' => '类别']);

				if ($validate->check($infos)) {
					$banner = bannerModel::create($infos);
					return ['code' => 1, 'msg' => '添加成功'];
				} else {
					return $validate->getError();
				}
			}else{
				return ['code' => 0, 'msg' => '请上传图片'];

			}

		}
		return $this->fetch();
	}

	/**
	 * 编辑轮播图
	 */
	public function editBanner()
	{
		if (request()->isPost()){
			$infos=input('post.');
			$files = input('file.');
			if(!empty($files)){
				$file = request()->file('titleFile');
				if($file){
					// 移动到框架uploads/ 目录下
					$info = $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif'])->move( './uploads');
					if($info){
						$infos['img']='/uploads/'.$info->getSaveName();
					}else{
						// 上传失败获取错误信息
						return $file->getError();
					}
				}
			}
			$infos['create_time']=time();
			$edit = bannerModel::where('id',$infos['id'])->update($infos);
			if($edit){
				return 1;
			}else{
				return 0;
			}
		}
		// 获取账号信息
		$id=input('get.id');
		$info = bannerModel::find($id);
		$this->assign('data',$info);
		return $this->fetch();

	}
	/**
	 * 删除轮播图
	 */
	public function delBanner()
	{
		$id=input('post.id');
		$del=bannerModel::where('id',$id)->delete();
		if($del){
			return ['code' => 1, 'msg' => '删除成功'];
		}else{
			return ['code' => 0, 'msg' => '删除失败'];
		}
	}
	/**
	 * 关闭网站
	 */
	public function close()
	{
		if(request()->isPost()){
			$info = input('post.');
			configModel::where('id',1)->update($info);
		}
		$con = configModel::find();
		$this->assign('con',$con);
		return $this->fetch();
	}
	/**
	 * 关于
	 */
	public function about()
	{
		$about = configModel::find();
		$abc=html_entity_decode($about['content']);
		$this->assign('about',$abc);
		return $this->fetch();
	}
	/**
	 * 修改关于
	 */
	public function addAbout()
	{

		if (request()->isPost()) {
			$info = input('post.');
			//验证token
			$rule = [
				'__token__'  =>  'require|token',
			];
			$msg = [
				'__token__.require' => '非法提交',
				'__token__.token'   => '请不要重复提交表单'
			];
			$validate   = \Validate::make($rule,$msg);
			if (!$validate->check($info)){
				$this->error($validate->getError());
			}
			unset($info['__token__']);
			$about=configModel::find();
			if($about == null){
				$add =  configModel::insert($info);
			}else{
				$add =  configModel::where('content',$about['content'])->update($info);
			}
			return $add;
		}
		return $this->fetch();

	}
	public function notice_list(){
		return $this->fetch();
	}
	public function notice_lists(){
		$limit = request()->param('limit');
		$list = NoticeModel::where('type', 1)->order('update_time desc')->paginate($limit);
		$count = NoticeModel::where('type', 1)->count();
		$data = ['code' => 0, 'msg' => '公告列表', 'count' => $count, 'data' => $list->items()];
		return json($data);
	}
	/**
	 * 添加公告
	 */
	public function addNotice()
	{

		if (request()->isPost()) {
			$info = input('post.');
			$info['type'] = 1;
			//导读限字
			$newStr = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $info['read']);
			$strlen=mb_strlen($newStr,"utf-8");//检测汉字的个数
			if($strlen>16){
				$this->error('导读请填写15字以内');
			}
			$add = NoticeModel::create($info);
			$this->success('公告添加成功');

		}
		return $this->fetch();

	}
	/**
	 * 修改公告
	 */
	public function editNotice()
	{
		if (request()->isPost()) {
			$info = input('post.');

			//导读限字
			$newStr = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $info['read']);
			$strlen=mb_strlen($newStr,"utf-8");//检测汉字的个数
			if($strlen>16){
				$this->error('导读请填写15字以内');
			}else{
				$img_obj=request()->file();
				if(isset($img_obj['titleFile'])){
					$file=$img_obj['titleFile'];
					$img = $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif'])->move('uploads');
					if ($img) {
						// 成功上传后 获取上传信息
						// 输出 42a79759f284b767dfcb2a0197904287.jpg
						$img_name = $img->getSaveName();
						$info['title_img']="/uploads/$img_name";
					} else {
						// 上传失败获取错误信息

						$img_name = $file->getError();
					}
				}

				$edit = NoticeModel::update($info);
				$this->success('公告添加成功');

			}
		}
		$id = input('get.id');
		$notice = NoticeModel::get($id);
		$this->assign('notice', $notice);
		return $this->fetch();
	}

	/**
	 * 内容图片
	 */
	public function uploadImg()
	{
		$files = request()->file();
		foreach ($files as $file) {
			// 移动到框架应用根目录/uploads/ 目录下
			$info = $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif,jpeg'])->move('uploads');
			if ($info) {
				// 成功上传后 获取上传信息
				// 输出 42a79759f284b767dfcb2a0197904287.jpg
				$img_name = $info->getSaveName();
			} else {
				// 上传失败获取错误信息
				$img_name = $file->getError();
			}
		}

		$data = ['code' => 0, 'msg' => $img_name, 'data' => ['src' => "/uploads/$img_name"]];
		return json($data);

	}

	/**
	 * 删除公告
	 */
	public function deleteNotice()
	{
		$id = input('post.id');
		$admin = NoticeModel::destroy($id);
		return $admin;
	}
	/**
	 * 短信配置
	 */
	public function sms_config()
	{
		$smso = SendmsgModel::where('id',1)->find();
		$this->assign('smso',$smso);
		return $this->fetch();

	}
	/**
	 * 操作短信配置
	 */
	public function dosms(){
		$data = input('post.');
		//提交数据
		$result = SendmsgModel::where('id',1)->update($data);
		//echo SendmsgModel::getLastSql();exit;
		if ($result){
			return array('code'=>1,'msg'=>'保存成功');
		} else {
			return array('code'=>0,'msg'=>'保存失败');
		}
	}
	/**
	 * 每天签到获得积分设置
	 */
	public function check_in()
	{
		$info = Reward::where('id',7)->find();
		$this->assign('info',$info);
		return $this->fetch();

	}
	public function check(){
		$info = input('tip');
		$result = Reward::where('group', 5)
			->update(['tip' => $info]);
		if($result){
			$data = ['code' => 1, 'msg' => '保存成功', ];
		}else{
			$data = ['code' => 0, 'msg' => '保存失败', ];
		}
		return $data;
	}
}